package com.vue.power.backend.common.base.controller;

import com.vue.power.backend.common.base.constant.SystemStaticConst;
import com.vue.power.backend.common.base.entity.Page;
import com.vue.power.backend.common.base.entity.QueryBase;
import com.vue.power.backend.common.base.service.BaseService;
import com.vue.power.backend.common.util.json.JsonUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseController<T extends QueryBase> {

    protected abstract BaseService<T> getService();

    /**
     * 功能描述：验证数据是否已经存在
     * @param entity
     * @return
     */
    @RequestMapping(value = "/isExist",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> isExist(@RequestBody T entity){
        Map<String,Object> result = new HashMap<String, Object>();
        if(getService().isExist(entity)){
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"数据已经存在！");
        }else{
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG,"验证通过！");
        }
        return result;
    }

    /**
     * 功能描述：保存数据字典数据
     * @param entity
     * @return
     */
    @RequestMapping(value = "/get",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> get(@RequestBody T entity) throws Exception{
        entity = getService().get(entity);
        Map<String,Object> result = new HashMap<String, Object>();
        if(entity!=null){
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG,"获取数据成功！");
            result.put("entity",entity);
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"获取数据失败！");
        }
        return result;
    }

    /**
     * 功能描述：保存数据字典数据
     * @param entity
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> save(@RequestBody T entity) throws Exception{
        boolean success = getService().save(entity);
        Map<String,Object> result = new HashMap<String, Object>();
        if(success==true){
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG,"增加数据成功！");
            result.put("entity",entity);
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"增加数据失败！");
        }
        return result;
    }

    /**
     * 功能描述：更新数据字典数据
     * @param entity
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> update(@RequestBody T entity)  throws Exception{
        boolean success  = getService().update(entity);
        Map<String,Object> result = new HashMap<String, Object>();
        if(success==true){
            result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG,"更新数据成功！");
            result.put("entity",entity);
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"更新数据失败！");
        }
        return result;
    }

    /**
     * 功能描述：实现批量删除数据字典的记录
     * @param entity
     * @return
     */
    @RequestMapping(value = "/remove",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> remove(@RequestBody T entity) throws Exception{
        Map<String,Object> result = new HashMap<String, Object>();
        getService().delete(entity);
        result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);
        result.put(SystemStaticConst.MSG,"删除数据成功！");
        return result;
    }


    /**
     * 功能描述：实现批量删除数据字典的记录
     * @param json
     * @return
     */
    @RequestMapping(value = "/removeBath",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> removeBath(@RequestBody String json) throws Exception{
        Map<String,Object> result = new HashMap<String, Object>();
        if(getService().removeBath((List<T>) JsonUtils.objToList(json,(Class <T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]))){
            result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG,"删除数据成功！");
        }else{
            result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG,"删除数据失败！");
        }
        return result;
    }

    /**
     * 功能描述：获取数据字典的分页的数据
     * @param entity
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> list(@RequestBody T entity){
        Map<String,Object> result = new HashMap<String, Object>();
        Page page = getService().findByPage(entity);
        result.put("totalCount",page.getTotal());
        result.put("result",page.getRows());
        return result;
    }

    /**
     * 功能描述：通用的查询方法
     * @param query
     * @return
     */
    @RequestMapping(value = "/commonQueryList",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> commonQueryList(@RequestBody Map<String,Object> query){
        Map<String,Object> result = new HashMap<String, Object>();
        Page page = getService().commonQueryList(query);
        result.put("totalCount",page.getTotal());
        result.put("result",page.getRows());
        return result;
    }



}
